home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
c
/
hsend.exe
/
BHSEND.C
< prev
next >
Wrap
Text File
|
1992-01-17
|
20KB
|
778 lines
#include <stdlib.h>
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <stat.h>
#include <conio.h>
#include <dir.h>
#include <dos.h>
#include <time.h>
#include <async.h>
#include <process.h>
#define HOST 1
#define GUEST 0
#define DATAFILE "CONFIGUR.DAT"
#define OFFLINE (!(a_getmsr(P)&CRX) && !(bioskey(2)&64))
#define TRY 3 /* number of attempts to resend garbled data block */
#define CRX 128 /* carrier detect */
#define OKTS 1 /* OK to send */
#define SFR 2 /* send file request */
#define IFR 3 /* initiate file receive */
#define IFS 4 /* initiate file send */
#define DF 5 /* delete file */
#define RP 6 /* request password */
#define SP 7 /* sending password */
#define EM 8 /* error message */
#define EOC 9 /* end of call */
#define ACK 10 /* acknowledgement */
#define NAK 11 /* not acknowledged */
#define SDB 12 /* sending data block */
void host_mode(void),beep(int,int),copyright(void),report(char *);
void delete_files(char *,int),error_message(char *),end_of_call(void);
void empty_box(int,int,int,int),tsr(unsigned int),b_sleep(int);
void interrupt background_host_mode(void);
void interrupt call(void);
int initiate_file_send(char *,char *),send_file_request(unsigned char *,int);
int init_modem(void),load_data(void),break_handler(void);
int password_check(void),scb(int,int,unsigned char *),rcb(int,unsigned char *),agetc(void);
int cmd_mode(int),seperate(char *,char *,char*,char);
int create_local_file(char *),rdb(int,unsigned char *);
char *eight_plus_three(char *,char);
int WAITING,BAUD,COMM,RINGS,SCREENFLAG,LOGFILEFLAG,FLAGFILEFLAG;
int I,C,TIMEOUT=180,IN_USE=0;
long TIMER;
char LOGFILE[61],FLAGFILE[61],PASSWORD[13];
unsigned char TEMP[518],BUF[518],IOBUF[518];
char SOURCE_PATHNAME[200],DEST_PATHNAME[200],FROM[200],TO[200];
char PATH[100],PATHNAME[116];
char SCREEN_BUFFER[644],RESTORE_BUFFER[644];
struct ffblk F,F1;
ASYNC *P=NULL;
struct address{
char far *p;};
struct address far *int63=(struct address far *) 252;
struct address far *int62=(struct address far *) 248;
struct address far *int28h=(struct address far *) 160;
struct tm *PTR;
time_t LT;
main(argc)
int argc;
{
load_data();
if(argc>1){
cprintf("\n\rUsage: bhsend <no arguments> Run in background host mode\n\r");
beep(3,400);
exit(1);}
if(int63->p!=int62->p){
cprintf("\n\rBHSEND is already running.\n\r");
exit(1);}
TIMEOUT*=2; /* host waits longer */
copyright();
if(!init_modem()){
gotoxy(1,21);
textcolor(LIGHTGRAY);
cprintf("\n\rCOMM device did not respond. Check settings with CONFIGUR.\n\r");
beep(3,400);
exit(1);}
gotoxy(22,12);
textcolor(LIGHTCYAN|BLINK);
cprintf("Background program is running, please wait");
gettext(20,8,65,14,SCREEN_BUFFER);
gotoxy(22,12);
cprintf(" ");
gotoxy(1,24);
textcolor(LIGHTGRAY);
cprintf("HSEND background mode has been loaded into memory.");
int62->p=int28h->p; /* move the old vector */
int28h->p=(char far *)background_host_mode; /* put in the new vector */
tsr(5473);
}
void interrupt background_host_mode(void) /* TSR enters here */
{
geninterrupt(62);
if(!OFFLINE && !IN_USE){
IN_USE++;
gettext(20,8,65,14,RESTORE_BUFFER);
puttext(20,8,65,14,SCREEN_BUFFER);
LT=time(NULL);
PTR=localtime(<);
strcpy(TEMP,"On-line: ");
strcat(TEMP,asctime(PTR));
report(TEMP);
if(password_check()) while(cmd_mode(1));
LT=time(NULL);
PTR=localtime(<);
strcpy(TEMP,"Modem Reset: ");
strcat(TEMP,asctime(PTR));
report(TEMP);
report("---------------------------------------------------------------------------\n\r");
a_setmcr(P,0); /* hang up */ /* set DTR and RTS high */
b_sleep(2);
a_setmcr(P,MCR_DTR+MCR_RTS); /* set DTR and RTS high */
b_sleep(1);
a_puts("+++",P);
b_sleep(2);
a_puts("\n\rATZ\n\r",P);
b_sleep(2);
strcpy(TEMP,"AT S0=1 V0 &D3\n\r");
TEMP[6]=RINGS+48; /* set correct value for S0 */
a_puts(TEMP,P);
C=a_icount(P);
for(I=0;I<C;I++)
a_getc(P);
while(!OFFLINE); /* wait here */
while(bioskey(1)) bioskey(0); /* empty keystroke buffer */
puttext(20,8,65,14,RESTORE_BUFFER);
IN_USE=0;
}
} /* TSR exits until next int28H call */
int cmd_mode(host_flag) /* return 0 at end of sesssion */
{
for(;;){
if(host_flag){
if(!scb(OKTS,0,NULL)){ /* OK to send */
report("ERROR- OKTS transmission not acknowledged");
return 0;}}
do{ /* receiving command */
if(OFFLINE){
report("DATA CONNECTION LOST");
return 0;}
C=rcb(0,BUF);
}while(!C);
switch(C){
case OKTS: return -1;
case SFR: send_file_request(BUF,0);
break;
case IFR: create_local_file(BUF);
break;
case DF: delete_files(BUF,HOST);
break;
case EM: error_message(BUF);
break;
case EOC: report("Transmission terminated normally");
return 0;
default: report("cmd_mode() default");
if(!host_flag) return -1;} /* switch back to active mode */
} /* endless loop */
}
void report(message)
char *message;
{
int fp;
if(!LOGFILEFLAG) return;
if((fp=_open(LOGFILE,O_WRONLY|O_APPEND))==-1){
if((fp=open(LOGFILE,O_CREAT,S_IWRITE))==-1){
putch('!');
return;}}
if(write(fp,message,strlen(message))<0) beep(3,300);
write(fp,"\n\r",2);
close(fp);
}
int init_modem(void)
{
int x;
if(P) a_close(P,0);
P=a_open(COMM,BAUD,0,8,1,1040,1040); /* open at no parity,8 dbits, 1 sbit 1K input ,1k output buffer */
a_setmcr(P,MCR_DTR+MCR_RTS); /* set DTR and RTS high */
b_sleep(2);
a_iflush(P); /* clear input buffer */
b_sleep(1);
a_puts("+++",P);
b_sleep(2);
a_puts("\n\rATZ\n\r",P);
b_sleep(2);
strcpy(TEMP,"AT s0=1 V0\n\r");
TEMP[6]=RINGS+48; /* set correct value for S0 */
a_puts(TEMP,P);
a_waitquiet(P,18,72,1); /* wait for a reply */
x=a_icount(P);
while(a_icount(P)) a_getc(P); /* clear input buffer */
return x; /* return # of characters in reply */
} /* 0 return value denotes no modem present */
int agetc(void) /* returns 0 if error occurs */
{
TIMER=biostime(0,0);
do{
if(OFFLINE) return -1; /* no carrier */
if(a_icount(P)) return a_getc(P);
}while(biostime(0,0)-TIMER<TIMEOUT);
report("Timeout");
return -2;
}
int password_check(void) /* return 1 if password is verified */
{
b_sleep(3);
if(!scb(RP,0,NULL)){
report("Caller could not verify Requesting Password transmission");
return 0;}
if(!rcb(SP,BUF)){
report("Caller did not sent password");
return 0;}
if(!strcmp(BUF,PASSWORD)) return 1; /* correct password received */
report("Incorrect password received.");
return 0;
}
int scb(cmd,blklen,blk) /* send command block, returns 1 if acknowledged */
int cmd; /* #defined command code */
int blklen; /* number of data characters */
unsigned char *blk; /* 0 to 512 data characters */
{ /* returns value of carrier detect */
int i,try,reply;
unsigned long cksum;
while(a_icount(P)) a_getc(P); /* clear input buffer */
IOBUF[3]=cmd; /* command code */
IOBUF[4]=blklen/255; /* # of data chars hibyte */
IOBUF[5]=blklen-(IOBUF[4]*255); /* # of data chars lobyte */
if(blklen){ /* if there are data characters */
for(i=0;i<blklen;i++) /* copy them into buf */
IOBUF[i+6]=blk[i];}
for(i=3,cksum=0;i<blklen+6;i++) /* add to create checksum */
cksum+=IOBUF[i];
IOBUF[0]=cksum/65025; /* checksum hibyte */
cksum-=(long)IOBUF[0]*65025;
IOBUF[1]=cksum/255; /* checksum midbyte */
IOBUF[2]=cksum-(long)(IOBUF[1]*255); /* checksum lobyte */
for(try=0;try<TRY;try++){
for(i=0;i<blklen+6;i++)
a_putc(IOBUF[i],P);